var config = require('../../utils/config.js');
var util = require('../../utils/util.js');
var API_URL = config.API_URL;
var app = getApp()
var page, loading, userInfo, requestTask, data;
var keyword = "";
var cate_id = "";
var region_id = "";
var goods_type = "";
var price_type = "";
var min_price = "";
var max_price = "";
var sort_by = "time";
var sort_type = "desc";
var URL = API_URL.goods_list;
var getData = that => {
    if (loading) {
        return
    }
    loading = true;
    page = page;
    data = {
        page: page,
        keywords: keyword,
        goods_type: goods_type,
        min_price: min_price,
        max_price: max_price,
        price_type: price_type,
        cate_id: cate_id,
        region_id: region_id,
        sort_by: sort_by,
        sort_type: sort_type,
    };
    requestTask = my.httpRequest({
        url: URL,
        data: data,
        method: 'POST',
        success: function (res) {
            console.log(res);
            var list = res.data.data.goods;
            var goods = that.data.goods;
            console.log(goods)
            console.log(list)
            if (page == 1) {
                goods = list;
            } else {
                goods = goods.concat(list);
            }
            var loading_text = that.data.loading;
            if (res.data.hasMore == 0) {
                loading_text = '没有更多了'
            } else {
                page++
            }
            that.setData({
                goods: goods,
                loading: loading_text
            })
        },
        fail: function (res) {
            console.log(res)
            // util.showNotice('请求超时');
        },
        complete: function (res) {
            loading = false;
            my.stopPullDownRefresh();
        },
    })
}
Page({
    data: {
        loading: '加载中...',
        goods: [],
        localtionLines: [],
        cityLines: [],
        provinceshow: false,
        cityshow: false,
        sortShow: false,
        sort: "排序",
        img: true,
        imgUrl: "/images/up.png",
        imgKey: true,
        yellow: 0,
        shade_show: false,
        babyType_id: 999,
        many_id: 999,
        price_id: 999,
        localid: "",
        themes: 999,
        keywords: "",
        min_price: "",
        max_price: "",
        provincename: "不限",
        cityname: "不限",
    },
    onLoad: function () {
        var that = this;
        page = 1;
        getData(that);
    },
    // 下拉加载 需要在真机上面调试
    onPullDownRefresh: function () {
        page = 1;
        loading = false;
        getData(this);
    },
    // 低部加载
    onReachBottom: function () {
        if (this.data.loading == "没有更多了") {
            return
        }
        keyword = keyword;
        goods_type = goods_type;
        min_price = min_price;
        max_price = max_price;
        price_type = price_type;
        cate_id = cate_id;
        region_id = region_id;
        sort_by = sort_by;
        sort_type = sort_type;
        getData(this);
        console.log(page)
    },
    // 去购物车
    cart: function (e) {
        my.navigateTo({
            url: '/pages/cart/cart',
        })
    },
    // 排序显示
    sortShows: function () {
        var sortShow = this.data.sortShow;
        this.setData({
            sortShow: !sortShow,
        })
    },
    // 排序筛选操作
    sortsh: function (e) {
        var that = this;
        var id = e.currentTarget.id;
        // true
        var imgKey = this.data.imgKey;
        console.log(imgKey)
        var sort, imgUrl;
        // 是最新的 则要拿到发布的时间 然后做排序
        my.showLoading({
            content: "正在加载中..."
        });
        if (id == 1) {
            sort = "最新";
            imgUrl = "/images/up_y.png";
            sort_by = "time";
            sort_type = "desc";
            console.log(data)
            this.setData({
                img: true,
                imgKey: true,
                loading: '加载中...',
            });
        } else if (id == 2) {
            sort = "价格";
            sort_by = "price";
            sort_type = "asc";
            if (!imgKey) {
                imgUrl = "/images/down.png";
                this.setData({
                    img: false,
                    imgKey: !imgKey,
                    loading: '加载中...',
                })
            } else {
                imgUrl = "/images/down_y.png";
                sort_by = "price";
                sort_type = "desc";
                this.setData({
                    img: false,
                    imgKey: !imgKey,
                    loading: '加载中...',
                })
            }
        } else if (id == 3) {
            sort = "收藏";
            imgUrl = "/images/up_y.png";
            sort_by = "collect";
            sort_type = "desc";
            this.setData({
                imgShow: true,
                img: true,
                imgKey: true,
                loading: '加载中...',
            })
        }
        var that = this;
        page = 1;
        data = {
            page: page,
            goods_type: goods_type,
            min_price: min_price,
            max_price: max_price,
            price_type: price_type,
            cate_id: cate_id,
            region_id: region_id,
            sort_by: sort_by,
            sort_type: sort_type,
        }
        console.log(data)

        URL = "https://v1.develop.yudu.docdic.com/api/mall";
        my.httpRequest({
            url: URL,
            data: data,
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                console.log(res);

                var goods = res.data.data.goods;
                console.log(goods)
                var loading_text = that.data.loading;
                if (res.data.hasMore == 0) {
                    loading_text = '没有更多了'
                } else {
                    page++
                }
                that.setData({
                    goods: goods,
                    loading: loading_text,
                    shade_show: false,
                    imgShow: true,
                    sortShow: false,
                    sort: sort,
                    imgUrl: imgUrl,
                })
            },
            fail: function (res) {
                console.log(res);
            },
            complete: function () {
                my.hideLoading();
            }
        });
        that.setData({
            sortShow: false,
            sort: sort,
            imgUrl: imgUrl
        })
    },
    // 寻找宝贝
    search: function (e) {
        console.log(e)
        var categoryId = '';
        page = 1;
        keyword = e.detail.value;
        sort_by = "time";
        sort_type = "desc";
        this.setData({
            loading: '加载中...',
            sort: "排序",
            localid: "",
            yellow: 0,
            imgUrl: "/images/up.png",
            img: true,
            imgKey: true,
            themes: 999,
            keywords: keyword,
            localid: "",
            price_id: 999,
            many_id: 999,
            min_price: "",
            max_price: "",
            babyType_id: 999,
            provincename: "不限",
            cityname: "不限",
        })
        my.showLoading({
            content: "正在加载",
            success: (res) => {
                getData(this);
                setTimeout(function () {
                    my.hideLoading();
                }, 2000);
            },
        });



    },
    // 显示商品的筛选
    screen: function () {
        var shade_show = this.data.shade_show;

        this.setData({
            sortShow: false,
            provinceshow: false,
            cityshow: false,
            shade_show: !shade_show,
        })
    },
    // 选择商品
    theme: function (e) {
        // 拿到点击的商品的ID 
        var id = e.currentTarget.id;
        console.log(e)
        if (id !== 0) {
            // 拿到点击的商品的ID  以便筛选
            cate_id = e.target.dataset.id
        }
        this.setData({
            themes: id
            // provinceshow: false,
            // cityshow: false
        })
    },
    // 选择价格
    Price: function (e) {
        console.log(e)
        var max;
        var min;
        var id = e.target.dataset.id;
        // 拿到选择价格的id 以便筛选
        price_type = id;
        this.setData({
            price_id: id
            // provinceshow: false,
            // cityshow: false,
        })
    },
    // 拿到输入的最小值
    inputmin: function (e) {
        min_price = e.detail.value

    },
    // 拿到输入的最大值
    inputmax: function (e) {
        max_price = e.detail.value
    },
    cs: function () {
        console.log("这里是为了可以点击输入")
    },
    // 拿到选择价格的值 和价格的id做显示
    textBtn: function (e) {
        console.log(e)
        var id = e.currentTarget.id;
        var max = e.target.dataset.max;
        var min = e.target.dataset.min;
        min_price = min;
        max_price = max,
            this.setData({
                many_id: id,
                min_price: min,
                max_price: max
                // provinceshow: false,
                // cityshow: false
            })
    },
    // 获取宝贝类型
    babyType: function (e) {
        console.log(e)
        var id = e.currentTarget.id;
        goods_type = id;
        this.setData({
            babyType_id: id
            // provinceshow: false,
            // cityshow: false
        })
    },
    // 获取所在地的省份和市区
    local: function (e) {
        var that = this;
        var localtion;
        var address_id = 1;
        var local_key = true;
        var id = e.currentTarget.id;
        var cityid = this.data.localid;
        // 选择省份
        if (id == 0) {
            console.log(this.data.localtionLines)
            if (this.data.localtionLines.length == 0) {
                localtion = {
                    address_id: 1,
                }
            } else {
                this.setData({
                    provinceshow: true,
                })
                return
            }
        } else if (cityid == "" || cityid == region_id) {
            return
        } else {
            if (this.data.cityLines.length == 0) {
                localtion = {
                    address_id: cityid,
                }
            } else {
                this.setData({
                    cityshow: true
                })
                return
            }
        }
        my.httpRequest({
            url: "https://v1.develop.yudu.docdic.com/api/mall/select/address",
            data: localtion,
            method: 'POST',
            headers: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                console.log(res)
                if (id == 0) {
                    var localtionLines = res.data.data
                    that.setData({
                        localtionLines: localtionLines,
                        provinceshow: true,
                    })
                } else {
                    var cityLines = res.data.data
                    that.setData({
                        cityLines: cityLines,
                        cityshow: true
                    })
                }
            },
            fail: function (res) {
            },
            complete: function () {
            }
        })
    },
    // 选择省份
    setprovince: function (e) {
        var localid = e.target.dataset.id;
        var provincename = e.target.dataset.name;
        this.setData({
            localid: localid,
            provincename: provincename,
            cityname: "不限",
            provinceshow: false,
            cityshow: false
        })
    },
    // 选择市区
    setcity: function (e) {
        region_id = e.target.dataset.id;
        var cityname = e.target.dataset.name;
        this.setData({
            cityname: cityname,
            cityshow: false,
        })
    },
    // 重置
    reset: function () {
        this.setData({
            themes: 999,
            localid: "",
            price_id: 999,
            many_id: 999,
            min_price: "",
            max_price: "",
            babyType_id: 999,
            provincename: "不限",
            cityname: "不限",
        })
    },
    // 确定
    confirms: function () {
        page = 1;
        var that = this;
        console.log(this.data)
        my.showLoading({
            content: "正在筛选中..."
        });
        if (region_id == "") {
            region_id = this.data.localid
        }
        data = {
            page: page,
            keywords: "",
            goods_type: goods_type,
            min_price: min_price,
            max_price: max_price,
            price_type: price_type,
            cate_id: cate_id,
            region_id: region_id,
            sort_by: sort_by,
            sort_type: sort_type,
        }
        this.setData({
            loading: '加载中...',
        })
        console.log(data)
        URL = "https://v1.develop.yudu.docdic.com/api/mall";
        my.httpRequest({
            url: URL,
            data: data,
            method: 'POST',
            headers: {
                'content-type': 'application/x-www-form-urlencoded',
            },
            success: function (res) {
                console.log(res)
                var goods = res.data.data.goods;
                var loading_text = that.data.loading;
                if (res.data.hasMore == 0) {
                    loading_text = '没有更多了'
                } else {
                    page++
                }
                that.setData({
                    goods: goods,
                    loading: loading_text,
                    keywords: "",
                    yellow: 1,
                    shade_show: false,
                })
            },
            fail: function (res) {
                console.log(res);
            },
            complete: function (res) {
                loading = false;
                my.stopPullDownRefresh();
                my.hideLoading();
            }
        });
    },
    // 跳转商品详情页
    goToGood: function (e) {
        console.log(e);
        var id = e.currentTarget.dataset.id;
        my.navigateTo({
            url: '/pages/good/good?id=' + id
        })
    },
})